<?php

namespace common\components;

use yii\base\InvalidConfigException;
use yii\base\NotSupportedException;
use common\components\Smser;

/**
 * Created by PhpStorm.
 * User: ai
 * Date: 2016/5/5
 * Time: 11:22
 */
class MandaoSmser extends Smser
{
    /**
     * @inheritdoc
     */
    public $url = 'http://sdk.entinfo.cn:8061/mdsmssend.ashx';

    /**
     * @inheritdoc
     */
    public function send($mobile, $content)
    {
        if (parent::send($mobile, $content)) {
            return true;
        }
        $data = ['sn' => $this->username, 'pwd' => strtoupper(MD5($this->username . $this->password)), 'mobile' => $mobile, 'content' => $content];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($ch);
        curl_close($ch);
        $this->state = $result;
        return $this->state;
    }

    /**
     * 设置密码
     *
     * @param string $password
     * @throws InvalidConfigException
     *
     * public function setPassword($password)
     * {
     * if ($this->username === null) {
     * throw new InvalidConfigException('用户名不能为空');
     * }
     *
     * $this->password =strtoupper(MD5($this->username . $this->password));
     * }*/

    /**
     * @inheritdoc
     */
    public function sendByTemplate($mobile, $data, $id)
    {
        throw new NotSupportedException('漫道科技不支持发送模板短信！');
    }
}
